<template>
  <div class="app-container">
    <!-- 顶部控制台 -->
    <div class="filter-container">
      <el-input class="filter-item" style="width: 200px; margin-left: 0px" size="mini" placeholder="请输入搜索的文案标题" v-model="form.searchTitle"/>
      <el-select class="filter-item" style="width: 160px" size="mini" v-model="form.searchClass" @change="handlePageInit">
        <el-option v-for="item in pageData.searchClassList" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-select class="filter-item" style="width: 120px" size="mini" v-model="form.searchVipStatus" @change="handlePageInit">
        <el-option v-for="item in pageData.searchVipStatusList" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-select class="filter-item" style="width: 120px" size="mini" v-model="form.searchStatus" @change="handlePageInit">
        <el-option v-for="item in pageData.searchStatusList" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-button type="primary" size="mini" icon="el-icon-search" @click="handlePageInit">搜索</el-button>
      <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleOpenSavePage">添加</el-button>
      <el-button type="success" size="mini" icon="el-icon-share" @click="handleBatchReleaseData" :disabled="form.selection.length < 1">批量发布</el-button>
    </div>
    <!-- 页面数据列表 -->
    <div class="data-table">
      <el-table :data="responseData.tableList"
                border style="width: 100%;"
                v-loading="pageData.loading"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                :header-cell-style="{ background: '#545C64', color: '#ffffff' }"
                @selection-change="handleTableSelection">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column fixed="left" prop="articleId" label="文案ID" width="320" align="center"/>
        <el-table-column prop="articleTitle" label="文案标题" width="320" align="center">
          <template slot-scope="scope">
            <span class="link-type" @click="handleOpenEditPage(scope)">{{ scope.row.articleTitle }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="articleClassId" label="所属板块" width="150" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.articleClassId === 5"><el-tag type="warning">亚利聊政采版块</el-tag></span>
            <span v-if="scope.row.articleClassId === 89"><el-tag type="success">政采之声</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="articleType" label="所属类型" width="120" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.articleType === 2"><el-tag type="primary">音频类型</el-tag></span>
            <span v-if="scope.row.articleType === 9"><el-tag type="danger">聊政采类型</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="articleIsVip" label="付费状态" width="80" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.articleIsVip === 0"><el-tag type="warning">否</el-tag></span>
            <span v-if="scope.row.articleIsVip === 1"><el-tag type="success">是</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="articleStatus" label="审核状态" width="80" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.articleStatus === 0"><el-tag type="warning">下架</el-tag></span>
            <span v-if="scope.row.articleStatus === 1"><el-tag type="success">发布</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="articleClickCount" label="点击量" width="100" align="center"/>
        <el-table-column prop="articleCreateTime" label="创建时间" width="160" align="center"/>
        <el-table-column prop="articleReleaseTime" label="发布时间" width="160" align="center"/>
        <el-table-column fixed="right" label="操作" width="220" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleOpenEditPage(scope)">编辑</el-button>
            <el-button v-if="scope.row.articleStatus === 0" size="mini" type="success" @click="handleUpdateDataStatus(scope, 1)">发布</el-button>
            <el-button v-if="scope.row.articleStatus === 1" size="mini" type="warning" @click="handleUpdateDataStatus(scope, 0)">下架</el-button>
            <el-button size="mini" type="danger" @click="handleDeleteData(scope)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页器-->
    <div style="margin-top: 20px">
      <div class="block" style="width: 100%; text-align: center">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="form.pageNum"
          :page-size="form.pageSize"
          :total="form.total"
          :page-sizes="[12, 50, 100, 200]"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  // 组件引入
  // 方法工具引入
  import { PermissionsToDetectFormUser } from '@/utils/PermissionsDetect';
  import { getPermission } from '@/utils/UserCookies';
  // 接口引入
  import { loadCmsArticleList,batchReleaseCmsArticleData,deleteCmsArticleData } from "@/api/content";
  export default {
    name: "audio",
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data (){
      return {
        pageData:{
          searchClassList: [
            {label: "全部分类", value: -1},
            {label: "亚利聊政采", value: 5},
            {label: "政采之声", value: 89}
          ],// 一级分类预设信息
          searchTypeStatus: false,// 二级分类是否可选标识
          searchTypeList: [
            {label: "全部类型", value: -1},
          ],// 类型筛选预设值
          searchVipStatusList: [
            {label: "付费状态", value: -1},
            {label: "免费", value: 0},
            {label: "付费", value: 1}
          ],// 付费状态预设项
          searchStatusList: [
            {label: "发布状态", value: -1},
            {label: "下架", value: 0},
            {label: "发布", value: 1}
          ],// 发布状态预设项
          loading: false,// 登录LOAD图
        },// 页面配置数据
        form:{
          queryScope: 2,
          searchTitle: "",
          searchClass: -1,
          searchType: -1,
          searchVipStatus: "",
          searchStatus: "",
          pageNum: 1,// 表单--页码
          pageSize: 12,// 表单--页面
          total: 0,// 表单--总量
          selection: []// 多选的内容
        },// 页面表单
        formRules: {
        },// 表单验证
        responseData: {
          tableList: [],// 页面表格数据
        },// 接口响应数据
      }
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods: {
      // 事件函数
      handlePageInit(){
        this.form.pageNum = 1;
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsArticleList();// 读取路由角色列表
      },// 页面初始化事件
      handleCurrentChange(val){
        this.form.pageNum = val;// 更换页码
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsArticleList();// 读取路由角色列表
      },// 页码切换事件
      handleSizeChange(val){
        this.form.pageSize = val;// 更换页面大小
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsArticleList();// 读取路由角色列表
      },// 页面size切换事件
      handleTableSelection(val){
        let list = [];// 初始化多选数组
        for (let i=0; i<val.length; i++){
          list.push(val[i].articleId);
        }// 遍历多选的列表，将ID拼入数组中
        this.form.selection = list;// 进行多选值的切换
      },// 表格多选事件
      handleSearchClass(){
        // 进行分类数据的判定
        if (this.form.searchClass !== -1){
          if (this.form.searchClass === 4){
            // 法律法规版块，二级菜单分类为4
            this.pageData.searchTypeStatus = false;
            this.form.searchType = -1;
          } else {
            // 案例点评版块，没有二级筛选菜单，禁止菜单选择，将二级选项设为空
            this.pageData.searchTypeStatus = true;
            this.form.searchType = -1;
          }
        } else {
          this.pageData.searchTypeStatus = true;
          this.form.searchType = -1;
        }
        this.handlePageInit();
      },// 一级分类筛选事件
      handleOpenSavePage(){
        if (PermissionsToDetectFormUser(getPermission(), "400001")){
          this.$jump.push({path: "/content/articleSave", query: {routerType: 2}});
        }  else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开新增页面
      handleOpenEditPage(scope){
        if (PermissionsToDetectFormUser(getPermission(), "400003")){
          let routeUrl = this.$router.resolve({
            path: "/content/articleEdit",
            query: {articleId: scope.row.articleId, routerType: 2}
          });
          window.open(routeUrl.href, '_blank');// 使用开启新窗口跳转
        }  else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开修改页面
      handleBatchReleaseData(){
        if (PermissionsToDetectFormUser(getPermission(), "400005")){
          this.$confirm("是否发布当前所选数据?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let param = {
              articleId: this.form.selection.toString(),
              articleStatus: 1
            };
            this.batchReleaseCmsArticleData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 批量发布数据
      handleUpdateDataStatus(scope, status){
        if (PermissionsToDetectFormUser(getPermission(), "460005")){
          this.$confirm("是否更改当前所选数据的发布状态?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let param = {
              articleId: scope.row.articleId,
              articleStatus: status
            };
            this.batchReleaseCmsArticleData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 更改内容发布状态
      handleDeleteData(scope){
        if (PermissionsToDetectFormUser(getPermission(), "400002")){
          // 付费内容--并且点击量不为0的内容无法删除
          if (scope.row.articleIsVip === 1 || parseInt(scope.row.articleClickCount) > 0 || scope.row.articleStatus === 1){
            this.$message.info("付费、发布、点击量大于0的内容暂不支持自动删除，请联系管理员");
          } else {
            // 进行文案内容的删除
            this.$confirm('是否删除当前所选数据?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              // 拼接访问参数
              let prams = {articleId: scope.row.articleId,};
              // 进行数据请求
              this.deleteCmsArticleData(prams);
            }).catch(cancel => {
              console.log(cancel);
              this.$message.info("已取消操作");
            });
          }
        }  else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 删除内容
      // 接口
      loadCmsArticleList(){
        loadCmsArticleList(this.form).then(response => {
          if (response.code === "200"){
            this.pageData.loading = false;
            this.responseData.tableList = response.data.list;
            this.form.total = response.data.total;
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 获取文章列表数据
      batchReleaseCmsArticleData(param){
        batchReleaseCmsArticleData(param).then(response => {
          if (response.code === "200"){
            this.$message.success(param.articleStatus === 1 ? "发布成功" : "下架成功");
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(close => {
          console.log(close);
        });
      },// 批量发布内容
      deleteCmsArticleData(param){
        deleteCmsArticleData(param).then(response => {
          if (response.code === "200"){
            this.$message.success("删除成功");
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(close => {
          console.log(close);
        });
      }// 删除文章数据
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created: function(){
      let _this = this;
      document.onkeydown = function(e) {
        if (!_this.pageData.loading) {
          let key = window.event.keyCode;
          if (key === 13) {
            _this.handlePageInit(); //此方法是当按下enter键后要做的动作。
          }
        }
      };
      setTimeout(function () {
        _this.handlePageInit();
      }, 800);
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {},
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  }
</script>

<style scoped>

</style>
